﻿using System.Data;
using System.Data.OleDb;
using MySql.Data.MySqlClient;


namespace Soufun.DB
{
    public enum DBType
    {
        eMySql,
        eAccess
    }
    public class DbHelper
    {
        public static DBType DbType = DBType.eMySql;


        public static int GetMaxID(string FieldName, string TableName)
        {
            if (DbType == DBType.eMySql)
            {
                return DbHelperMySQL.GetMaxID(FieldName, TableName);
            }
            else
            {
                return DbHelperOleDb.GetMaxID(FieldName, TableName);
            }
        }

        public static bool Exists(string strSql)
        {
            if (DbType == DBType.eMySql)
            {
                return DbHelperMySQL.Exists(strSql);
            }
            else
            {
                return DbHelperOleDb.Exists(strSql);
            }
        }


        public static DataSet Query(string sql)
        {
            if (DbType == DBType.eMySql)
            {
                return DbHelperMySQL.Query(sql);
            }
            else
            {
                return DbHelperOleDb.Query(sql);
            }
        }

        public static int ExecuteSql(string sql)
        {
            if (DbType == DBType.eMySql)
            {
                return DbHelperMySQL.ExecuteSql(sql);
            }
            else
            {
                return DbHelperOleDb.ExecuteSql(sql);
            }
        }

        //string roleName = "Tester";
        //int sdd = DbHelper.ExecuteSql("update trole set roleName =@1  where id = 5", new object[] { roleName });

        public static int ExecuteSql(string sqlString, object[] cmdParms)
        {
            if (DbType == DBType.eMySql)
            {
                MySqlParameter[] parameters = GetMysqlParameters(cmdParms);
                return DbHelperMySQL.ExecuteSql(sqlString, parameters);
            }
            else
            {
                OleDbParameter[] parameters = GetOleDbParameters(cmdParms);

                return DbHelperOleDb.ExecuteSql(sqlString, parameters);
            }
        }




        private static OleDbParameter[] GetOleDbParameters(object[] cmdParms)
        {
            OleDbParameter[] parameters = new OleDbParameter[cmdParms.Length];
            int i = 0;
            foreach (object parm in cmdParms)
            {
                OleDbParameter mySqlParameter = new OleDbParameter("@" + i.ToString(), parm);
                parameters[i] = mySqlParameter;
                i++;
            }
            return parameters;
        }

        private static MySqlParameter[] GetMysqlParameters(object[] cmdParms)
        {
            MySqlParameter[] parameters = new MySqlParameter[cmdParms.Length];
            int i = 0;
            foreach (object parm in cmdParms)
            {
                MySqlParameter mySqlParameter = new MySqlParameter("@" + i.ToString(), parm);
                parameters[i] = mySqlParameter;
                i++;
            }
            return parameters;
        }
    }
}
